package com.wisemoresystem.seal.action.seal.auth;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.base.tip.Tip;
import com.wisemoresystem.seal.model.t_seal_auth;
import com.wisemoresystem.seal.model.t_user;
import com.wisemoresystem.seal.service.AuthService;
import com.wisemoresystem.seal.web.WebTools;

/**
 * 被授权人员管理
 * 
 * @author Administrator
 *
 */
@Controller("ixSealAuth")
@Scope("prototype")
@RequestMapping("/ix/seal/auth")
public class SealAuthAction extends ProjBaseAction {

	@Autowired
	private AuthService authService;

	/**
	 * 被授权人员列表
	 * 
	 * @param srh_name
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(String srh_name) {
		List<String> ulist = authService.selectNotAuth();
		if (ChkTools.isNotNull(ulist)) {
			for (String user_id : ulist) {
				t_user user = oc.findObjById(user_id, t_user.class);
				user.setUser_level("非主管");
				oc.updateObj(user);
			}
		}

		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(System.currentTimeMillis());
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String today = df.format(c.getTime());
		List<Map<String, Object>> list = authService.selectSealAuthList(
				srh_name, today, page);
		List<Map<String, Object>> pow = authService.selectEmpower();
		int num = (Integer) pow.get(0).get("num");

		ModelAndView mv = new ModelAndView("/jsp/seal/auth/list");
		mv.addObject("list", list);
		mv.addObject("num", num);
		mv.addObject("user_id", WebTools.getCurrentUserId());

		return mv;
	}

	/**
	 * 授权人员信息
	 * 
	 * @return
	 */
	@RequestMapping("/add_form")
	public ModelAndView add_form() {
		List<Map<String, Object>> user = authService.selectUserRoleList();
		List<Map<String, Object>> userlist = authService.selectSealUserList();

		ModelAndView mv = new ModelAndView("/jsp/seal/auth/add_form");
		mv.addObject("user", user.get(0));
		mv.addObject("userlist", userlist);
		return mv;
	}

	/**
	 * 添加授权
	 * 
	 * @param auth
	 *            授权信息
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView add(t_seal_auth auth) {
		if (ChkTools.isNull(auth.getTime_get())) {
			auth.setTime_get(new Timestamp(System.currentTimeMillis()));
		}
		auth.setBank_id(WebTools.getCurrentBankId());
		auth.setFlag("生效");
		auth.setUser_id(WebTools.getCurrentUserId());
		oc.saveObj(auth);

		t_user user = oc.findObjById(auth.getEnd_user_id(), t_user.class);
		user.setUser_level("三级主管");
		oc.updateObj(user);

		logT.logH("添加印章授权", t_seal_auth.class, auth.get_id());

		super.setTipMsg("授权成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	/**
	 * 删除授权
	 * 
	 * @param auth_id
	 *            授权id
	 * @throws IOException
	 */
	@RequestMapping("/del")
	public void del(String auth_id) throws IOException {

		t_seal_auth auth = oc.findObjById(auth_id, t_seal_auth.class);
		auth.setFlag("失效");
		oc.updateObj(auth);
		t_user user = oc.findObjById(auth.getEnd_user_id(), t_user.class);
		user.setUser_level("非主管");
		oc.updateObj(user);

		logT.logH("印章授权失效", t_seal_auth.class, auth.get_id());

		resp.getWriter().print("t");
		resp.getWriter().flush();
	}

}
